part of '../../repository.dart';

const _kYookassaTransactionRevocationInfoRefundedAmountKey = 'refunded_amount';
const _kYookassaTransactionRevocationInfoTimeKey = 'time';

class _YookassaTransactionRevocationInfoDto extends Equatable {
  const _YookassaTransactionRevocationInfoDto._internal({
    required this.timeDto,
    required this.refundedAmountDto,
  });

  final _TimeDto timeDto;
  final _MoneyAmountDto refundedAmountDto;

  @override
  List<Object?> get props {
    return [
      timeDto,
      refundedAmountDto,
    ];
  }

  factory _YookassaTransactionRevocationInfoDto.fromJson(Map<String, dynamic> json) {
    final timeString = json[_kYookassaTransactionRevocationInfoTimeKey] as String;
    final timeDto = _TimeDto.fromString(timeString);

    final refundedAmountJson = json[_kYookassaTransactionRevocationInfoRefundedAmountKey];
    final refundedAmountDto = _MoneyAmountDto.fromJson(refundedAmountJson);

    return _YookassaTransactionRevocationInfoDto._internal(
      timeDto: timeDto,
      refundedAmountDto: refundedAmountDto,
    );
  }

  factory _YookassaTransactionRevocationInfoDto.fromEntity(
      YookassaTransactionRevocationInfo revocationInfo) {
    final timeDto = _TimeDto.fromEntity(revocationInfo.time);
    final refundedAmountDto = _MoneyAmountDto.fromEntity(revocationInfo.refundedAmount);

    return _YookassaTransactionRevocationInfoDto._internal(
      timeDto: timeDto,
      refundedAmountDto: refundedAmountDto,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      _kYookassaTransactionRevocationInfoTimeKey: timeDto.toString(),
      _kYookassaTransactionRevocationInfoRefundedAmountKey: refundedAmountDto.toJson(),
    };
  }

  YookassaTransactionRevocationInfo toEntity() {
    final time = timeDto.toEntity();
    final refundedAmount = refundedAmountDto.toEntity();

    return YookassaTransactionRevocationInfo(
      time: time,
      refundedAmount: refundedAmount,
    );
  }
}
